home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Viewers / aMiPEG_1.0 / src / cybergfx.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  7KB  |  258 lines

  1.  
  2. /* this source implements aMiPEG's interface to CyberGraphX */
  3.  
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. #include <exec/exec.h>
  9. #include <cybergraphics/cybergraphics.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/screens.h>
  12. #include <dos/dos.h>
  13.  
  14. #include <cybergraphics/cgxvideo.h>
  15. #include <pragmas/cgxvideo_pragmas.h>
  16. #include <clib/cgxvideo_protos.h>
  17.  
  18. #include <proto/graphics.h>
  19. #include <proto/exec.h>
  20. #include <proto/cybergraphics.h>
  21. #include <proto/intuition.h>
  22. #include <proto/dos.h>
  23. #include <proto/asyncio.h>
  24.  
  25. #include "video.h"
  26. #include "proto.h"
  27.  
  28. extern int doneFlag;
  29. extern int fullFlag;
  30. extern int ditherType;
  31. extern int cyber_pub;
  32. extern struct GfxBase *GfxBase;
  33.  
  34. struct Library *CyberGfxBase;
  35. struct Library *CGXVideoBase;
  36.  
  37. ULONG cyber_mode;
  38. ULONG depth;
  39. struct Screen *cyber_screen;
  40. struct Window *cyber_window;
  41. int original_x, original_y;
  42. VLayerHandle VLH;
  43.  
  44.  
  45. static void Quit(char *why, int failcode)
  46. {
  47.     puts(why);
  48.     exit(failcode);
  49. }
  50.  
  51. static void output_term(void)
  52. {
  53.     close_timer();
  54.  
  55.     if(input)
  56.     {
  57.         CloseAsync(input);
  58.         input = NULL;
  59.     }
  60.  
  61.     if(VLH)
  62.     {
  63.         DetachVLayer(VLH);
  64.         DeleteVLayerHandle(VLH);
  65.     }
  66.  
  67.     if(cyber_window)
  68.         CloseWindow(cyber_window);
  69.  
  70.     if(!cyber_pub && cyber_screen)
  71.         CloseScreen(cyber_screen);
  72.  
  73.     if(CGXVideoBase)
  74.         CloseLibrary(CGXVideoBase);
  75.  
  76.     if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  77.     if (CyberGfxBase) CloseLibrary(CyberGfxBase);
  78.     if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
  79. }
  80.  
  81. void DrawCyberGfxImage(UWORD *data, int x, int y)
  82. {
  83.     int win_x, win_y, i;
  84.  
  85.     if(ditherType == CYBERGFXVLAYER_DITHER || ditherType == CYBERGFXVLAYERGRAY_DITHER)
  86.     {
  87.         if(LockVLayer(VLH))
  88.         {
  89.             UWORD *address = (UWORD *)GetVLayerAttr(VLH, VOA_BaseAddress);
  90.  
  91.             for(i = 0; i < y; i++)
  92.             {
  93.                 memcpy(address, data, x << 1);
  94.                 address += x;
  95.                 data += ((x + 15) >> 4) << 4;
  96.             }
  97.  
  98.             UnlockVLayer(VLH);
  99.  
  100.             return;
  101.         }
  102.     }
  103.  
  104.     win_x = cyber_window->Width - cyber_window->BorderLeft - cyber_window->BorderRight;
  105.     win_y = cyber_window->Height - cyber_window->BorderTop - cyber_window->BorderBottom;
  106.  
  107.     if(win_x == x && win_y == y)
  108.     {
  109.         if(ditherType == CYBERGFXGRAY_DITHER)
  110.             WritePixelArray(data, 0, 0, x & 0x0f ? ((x >> 4) + 1 ) << 4 : x, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, x, y, RECTFMT_GREY8);
  111.         else
  112.             WritePixelArray(data, 0, 0, (x & 0x0f ? ((x >> 4) + 1 ) << 4 : x ) * 4, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, x, y, RECTFMT_ARGB);
  113.     }
  114.     else
  115.     {
  116.         if(ditherType == CYBERGFXGRAY_DITHER)
  117.             ScalePixelArray(data, x, y, x & 0x0f ? ((x >> 4) + 1 ) << 4 : x, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, win_x, win_y, RECTFMT_GREY8);
  118.         else
  119.             ScalePixelArray(data, x, y, (x & 0x0f ? ((x >> 4) + 1 ) << 4 : x) * 4, cyber_window->RPort, cyber_window->BorderLeft, cyber_window->BorderTop, win_x, win_y, RECTFMT_ARGB);
  120.     }
  121. }
  122.  
  123. int handle_window(void)
  124. {
  125.     struct IntuiMessage *imsg;
  126.     struct IntuiMessage msg;
  127.     static ULONG last_secs, last_micros;
  128.  
  129.     if(cyber_window)
  130.     {
  131.         while(imsg = (struct IntuiMessage *)GetMsg(cyber_window->UserPort))
  132.         {
  133.             CopyMem((char *)imsg, (char *)&msg, (long)sizeof(struct IntuiMessage));
  134.             ReplyMsg((struct Message *)imsg);
  135.             switch(msg.Class)
  136.             {
  137.                 case IDCMP_VANILLAKEY:
  138.                     if(msg.Code == '\x1b' || msg.Code == 'q')
  139.                     {
  140.                                             doneFlag = DONE_REALLY;
  141.                                             return(FALSE);
  142.                     }
  143.                     if(msg.Code == 13 || msg.Code == 32)    /* space & return */
  144.                         return(FALSE);
  145.                     break;
  146.  
  147.                 case IDCMP_CLOSEWINDOW:
  148.                 {
  149.                     doneFlag = DONE_NOT_REALLY;
  150.                     return(FALSE);
  151.                 }
  152.  
  153.                 case IDCMP_MOUSEBUTTONS:
  154.                     if(!fullFlag && msg.Code == SELECTDOWN)
  155.                     {
  156.                         if(DoubleClick(last_secs, last_micros, msg.Seconds, msg.Micros))
  157.                             ChangeWindowBox(cyber_window, cyber_window->LeftEdge, cyber_window->TopEdge, original_x, original_y);
  158.  
  159.                         last_secs = msg.Seconds;
  160.                         last_micros = msg.Micros;
  161.                     }
  162.                 break;
  163.  
  164.                 default:
  165.                 break;
  166.             }
  167.         }
  168.     }
  169.     return(TRUE);
  170. }
  171.  
  172.  
  173. BOOL init_cybergfx(void)
  174. {
  175.     if(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",37))
  176.         if(CyberGfxBase = OpenLibrary("cybergraphics.library", 40))
  177.             if(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39))
  178.                 if(ditherType == CYBERGFXVLAYER_DITHER || ditherType == CYBERGFXVLAYERGRAY_DITHER)
  179.                 {
  180.                     if(CGXVideoBase = OpenLibrary("cgxvideo.library", 41))
  181.                         return(TRUE);
  182.                 }
  183.                 else
  184.                     return(TRUE);
  185.             else
  186.                 Quit("graphics.library v39+ not present", 25);
  187.         else
  188.             Quit("cybergraphics.library v40+ not present", 25);
  189.     else
  190.         Quit("intuition.library is too old, <V39", 25);
  191. }
  192.  
  193. int get_cyber_mode(void)
  194. {
  195.     return((int)CModeRequestTags(NULL,
  196.                                                     CYBRMREQ_WinTitle, "Select a screenmode for MPEG window",
  197.                                                     CYBRMREQ_MinDepth, 15,
  198.                                                     TAG_DONE));
  199. }
  200.  
  201.  
  202. unsigned long InitCyberGfxDisplay(unsigned long modeid)
  203. {
  204.     UWORD pens[] = { ~0 };
  205.  
  206.     atexit(output_term);
  207.  
  208.     if(init_cybergfx())
  209.     {
  210.         if(cyber_pub || (modeid != 0xffffffff) || (modeid = get_cyber_mode()) != 0xffffffff)
  211.         {
  212.             if(cyber_pub)
  213.             {
  214.                 cyber_screen = LockPubScreen(pubname_ptr);
  215.                 modeid = GetVPModeID(&cyber_screen->ViewPort);
  216.                 ScreenToFront(cyber_screen);
  217.                 UnlockPubScreen(NULL, cyber_screen);
  218.             }
  219.  
  220.             cyber_mode = modeid;
  221.             depth = GetCyberIDAttr(CYBRIDATTR_DEPTH, cyber_mode);
  222.             if(depth < 15 && ditherType != CYBERGFXVLAYER_DITHER && ditherType != CYBERGFXVLAYERGRAY_DITHER)
  223.                 Quit("aMiPEG: this screen is not deep enough (needed at least 15 bits).", 25);
  224.  
  225.             if(cyber_screen || (cyber_screen = OpenScreenTags(NULL,
  226.                             SA_Width, STDSCREENWIDTH,
  227.                             SA_Height, STDSCREENHEIGHT,
  228.                             SA_DisplayID, cyber_mode,
  229.                             SA_Title, (UBYTE *)"aMiPEG 1.0 by Michael Rausch and Miloslaw Smyk",
  230.                             SA_Pens, &pens,
  231.                             SA_SysFont, 1,
  232.                             SA_Depth, depth,
  233.                             TAG_DONE)))
  234.             {
  235.  
  236.  
  237.                 if(cyber_window = OpenWindowTags(NULL,
  238.                                     cyber_pub ? WA_PubScreenName : WA_CustomScreen, cyber_pub ? pubname_ptr : cyber_screen,
  239.                                     WA_Flags, WFLG_ACTIVATE | (fullFlag ? WFLG_BORDERLESS : WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM),
  240.                     WA_IDCMP, CLOSEWINDOW | MOUSEBUTTONS | VANILLAKEY,
  241.                     (fullFlag ? TAG_SKIP : TAG_IGNORE), 8,
  242.                     WA_InnerWidth, 160,
  243.                     WA_InnerHeight,120,
  244.                     WA_MaxWidth, ~0,
  245.                     WA_MaxHeight, ~0,
  246.                     WA_MinWidth, 60,
  247.                     WA_MinHeight, 60,
  248.                     WA_Title, "Playing MPEG animation",
  249.                     WA_ScreenTitle, (UBYTE *)"aMiPEG 1.0 by Michael Rausch and Miloslaw Smyk",
  250.                     TAG_DONE))
  251.  
  252.                     return(modeid);
  253.             }
  254.         }
  255.     }
  256.     Quit("aMiPEG: unable to open screen/window...", 25);
  257. }
  258.